package com.doit.mr.day04.movie;

import com.alibaba.fastjson.JSON;
import com.doit.mr.day04.bean.MovieWritable;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * @DATE 2021/12/9/23:02
 * @Author MDK
 * @Version 2021.2.2
 *  每部电影评分最高的前三条记录
 *
 **/
public class TopN {
    static class TopNMapper extends Mapper<LongWritable, Text, Text, MovieWritable>{
        Text k = new Text();
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            String line = value.toString();
            MovieWritable mw = JSON.parseObject(line, MovieWritable.class);
            //获取电影id
            String mid = mw.getMovie();
            k.set(mid);
            context.write(k,mw);
        }
    }
    //如果最终的输出结果不是电影均分的需求 而是电影的记录 输出的结果没必要K V  所以输出的可以是NullWritable  没有输出内容
    static class AvgRateReducer extends Reducer<Text, MovieWritable, MovieWritable, NullWritable> {
        @Override
        protected void reduce(Text key, Iterable<MovieWritable> values, Context context) throws IOException, InterruptedException {
            //创建一个存储记录的集合,迭代当前所有影评 并添加在集合中
            List<MovieWritable> list = new ArrayList<>();
            for (MovieWritable mw : values) {
                list.add(mw);
            }
            Collections.sort(list, new Comparator<MovieWritable>() {
                @Override
                public int compare(MovieWritable o1, MovieWritable o2) {
                    return Double.compare(o1.getRate(), o2.getRate());
                }
            });
            if(list != null & list.size()>0){
                //输出前3条记录
                for (int i = 0; i < Math.min(3, list.size()); i++) {
                    MovieWritable movieWritable = list.get(i);
                    context.write(movieWritable, NullWritable.get());
                }
            }
        }
    }

    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf, "topn");
        job.setMapperClass(AvgRate.AvgRateMapper.class);
        job.setReducerClass(AvgRate.AvgRateReducer.class);

        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(MovieWritable.class);
        job.setOutputKeyClass(MovieWritable.class);
        job.setOutputValueClass(NullWritable.class);

        FileInputFormat.setInputPaths(job, new Path("D:\\mrdata\\movie\\input"));
        FileOutputFormat.setOutputPath(job, new Path("D:\\mrdata\\movie\\output_01"));
        job.waitForCompletion(true);
    }
}
